package org.thunms.demo.test.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thunms.demo.test.entity.Products;
import org.thunms.demo.test.service.PlaceService;
import org.thunms.demo.test.service.TestService;

import com.alibaba.common.lang.StringUtil;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private PlaceService placeService;
	@Autowired
	private TestService testService;
	
	@RequestMapping
    public String index(){
		return "redirect:/admin/index.do";
    	
    }
	
	@RequestMapping(value="/init")
	public String place(){
		//在这里设置初始化信息
		//初始化城市分类信息
		placeService.init();
		return "admin/success";
		
	}
	
	@RequestMapping(value="/add")
	public String add(){
		
		return "admin/add";
	}
	@RequestMapping(value="/index")
	public String adminProds(String place,HttpServletRequest request,HttpServletResponse response){
		HttpSession session = request.getSession();
		List prods = testService.findByPlace(place);
		session.setAttribute("prods", prods);
		return "admin/admin";
	}
	
	@RequestMapping(value="/find")
	public String find(String inputurl,HttpServletRequest request,HttpServletResponse response){
		
		String start = "id=";
		String nePrice = "";
		String oldPrice = "";
		
		int i = inputurl.indexOf(start);
		String num_iid = inputurl.substring(i+3, i+14);
		//商品id
		Document result = APITest.testItemGet(num_iid);
		
		if(result!=null){
			Element item=result.getRootElement().element("item");
			//商品图片地址
			String pic = item.elementText("pic_url");
			//商品名称
			String title = item.elementText("title");
			//商品价格
			String price = item.elementText("price");
			System.out.println(price);

			//打折后价格
			Document promotion = APITest.PromotionGet(num_iid);
			if(promotion!=null){
				try{		
					String proprice =promotion.getRootElement().element("promotions").element("promotion_in_item").element("promotion_in_item").elementText("item_promo_price");					
					if(StringUtil.isNotBlank(proprice)){
						nePrice = proprice;
						oldPrice = price;	
						}	
				}catch(Exception e){
					nePrice = price;
					oldPrice = "";
				}							
			}
			
			
			request.setAttribute("picture", pic);
		
			request.setAttribute("title",title);
			request.setAttribute("price",nePrice);
			request.setAttribute("url",inputurl);
			return "admin/add";
		}
		return "admin/err";
		
		
		
		
		
	}
	
	@RequestMapping(value="/save")
	public String save(String title,String price,String url,String fenlei,String where,String name,String picture){
		
		Products prods = new Products();
		prods.setId(UUID.randomUUID().toString());
		prods.setName(title);
		prods.setPlace(where);
		prods.setPrice(price);
		prods.setUrl(url);
		prods.setType(fenlei);
		prods.setPicture(picture);
		prods.setReason(name);
		
		
		
		this.testService.saveProducts(prods);
		return "admin/add";
	}
	@RequestMapping(value="/admin")
	public String admin(String place,HttpServletRequest request,HttpServletResponse response){
		System.out.println(place);
		List prods = this.testService.findByPlace(place);
		request.setAttribute("prods", prods);
		return "admin/list";
	}
	

}
